package com.liik.game.mvc.object;

import android.graphics.Rect;
import android.view.SurfaceHolder;

import com.liik.game.util.Vector;

public class Display {
	// upper left visible tile
	public final Vector offset = new Vector();
	// number of displayed tiles
	public final Vector dimensions = new Vector();
	// size of screen in pixel
	public final Vector screensize = new Vector();
	// size of a tile
	public final float tileToPixel;
	// max scroll speed
	public final float scrollSpeed;

	public Display(float tileToPixel, float scrollSpeed, SurfaceHolder surfaceHolder) {
		this.tileToPixel = tileToPixel;
		this.scrollSpeed = scrollSpeed;
		Rect surfaceFrame = surfaceHolder.getSurfaceFrame();
		setScreenSize(surfaceFrame.width(), surfaceFrame.height());
	}

	public void setScreenSize(float width, float height) {
		screensize.set(width, height);
		dimensions.set(screensize);
		dimensions.div(tileToPixel);
	}

	private Vector scrollHelper = new Vector();

	public void scrollTo(Vector desiredCenter) {
		scrollHelper.set(offset);
		scrollHelper.x += (dimensions.x / 2);
		scrollHelper.y += (dimensions.y / 2);
		scrollHelper.setDirection(scrollHelper, desiredCenter);
		if (scrollHelper.length() > 0) {
			scrollHelper.set(desiredCenter);
			scrollHelper.x -= (dimensions.x / 2);
			scrollHelper.y -= (dimensions.y / 2);
			scrollHelper.setDirection(offset, scrollHelper);
			if (scrollHelper.length() > scrollSpeed) {
				scrollHelper.normalize();
				scrollHelper.mul(scrollSpeed);
			}
			offset.add(scrollHelper);
		}
	}

	public void screenToTile(Vector vector) {
		vector.div(tileToPixel);
		vector.add(offset);
	}
}
